package com.imaker.frame.base

import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.gyf.immersionbar.ktx.immersionBar
import com.imaker.frame.utils.ActManager
import com.imaker.frame.utils.LoadingViewUtil
import org.greenrobot.eventbus.EventBus


/**
 * @Description:
 * @author ziheng
 * @date 2022/12/5 11:01
 */
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {

    val TAG: String = this.javaClass.simpleName
    lateinit var mBind: T

    abstract fun getLayoutID(): Int

    abstract fun init()
    abstract fun observe()

    /**
     * 是否使用 EventBus
     */
    open fun useEventBus(): Boolean = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ActManager.Instance().pushActivity(this)
        Log.d(TAG, "onCreate()")
        mBind = DataBindingUtil.setContentView(this, getLayoutID())
        initImmersionBar()
        if (useEventBus()) {
            EventBus.getDefault().register(this)
        }
        observe()
        init()
    }

    private fun setGrayTheme() {
        val paint = Paint()
        val cm = ColorMatrix()
        cm.setSaturation(0F)
        paint.colorFilter = ColorMatrixColorFilter(cm)
        window.decorView.setLayerType(View.LAYER_TYPE_HARDWARE, paint)
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart()")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.d(TAG, "onRestoreInstanceState()")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume()")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause()")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop()")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d(TAG, "onSaveInstanceState()")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy()")
        mBind.unbind()
        if (useEventBus()) {
            EventBus.getDefault().unregister(this)
        }
        ActManager.Instance().popActivity(this)
    }

    fun showLoadingDialog() {
        Log.d(TAG, TAG + "showLoadingDialog()")
        LoadingViewUtil.showLoadingDialog(this)
    }

    fun dismissLoadingDialog() {
        Log.d(TAG, TAG + "dismissLoadingDialog()")
        LoadingViewUtil.dismissLoadingDialog()
    }

    open fun initImmersionBar() {
        immersionBar()
    }

}